package com.dz.finance.biz.service.impl;

import com.dz.finance.biz.constant.MsgConstant;
import com.dz.finance.biz.entity.MemberBindPhone;
import com.dz.finance.biz.mapper.MemberBindPhoneMapper;
import com.dz.finance.biz.service.MemberBindPhoneService;
import com.dz.finance.common.exception.BizException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * @author: DengHaoQi
 * @date: 2024/5/29 20:19
 * @description:
 */

@Service
@RequiredArgsConstructor
public class MemberBindPhoneServiceImpl implements MemberBindPhoneService {

    private final MemberBindPhoneMapper memberBindPhoneMapper;
    private final PasswordEncoder passwordEncoder;


    /*
    * 查询 MemberBindPhone
    * */
    @Override
    public MemberBindPhone getMemberBindPhone(MemberBindPhone mbp) {
        return memberBindPhoneMapper.selectMemberBindPhone(mbp);
    }


    /*
    * 注册
    * */
    @Override
    public void reg(Long memberId, String phone, String password) {
        MemberBindPhone mbp = new MemberBindPhone();
        mbp.setPhone(phone);
        mbp.setPassword(passwordEncoder.encode(password));
        mbp.setMemberId(memberId);
        mbp.initDefault();
        int row = memberBindPhoneMapper.insert(mbp);
        if (row == 0) {
            throw new BizException(MsgConstant.MEMBER_BIND_PHONE_REG_ERROR);
        }
    }
}
